#include <map>
#include <string>
#include <iostream>
#include <cmath>

class LangModel {
private:
  typedef std::map<std::string,size_t> LangModelType;
  LangModelType lm;
  size_t total;

public:
  void add(const std::string& w, size_t c) 
  {
    lm[w] = c;
    total += c;
  }

  size_t get(const std::string& w) const
  {
    LangModelType::const_iterator it = lm.find(w);
    if (it == lm.end()) {
      return 0;
      //lm[w] = 1;
      //++total;
    } else {
      return it->second;
    }
  }

  void save(std::ostream &os) const
  {
    os << "<lm t=\""<<total<<"\" w=\""<<lm.size()<<"\">" << std::endl;

    for(LangModelType::const_iterator it = lm.begin(); it != lm.end(); ++it) {
      size_t c = it->second;
      double p = log10(c)-log10(total);
      os << "<w c=\""<<c<<"\" p=\""<<p<<"\">"<<it->first<<"</w>" << std::endl;
    }

    os << "</lm>" << std::endl;
  }
};
